<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar">
        <!-- 这里是状态栏 -->
      </view>
      <view class="jtop_l" @tap="navigateBack">
        <image src="../../static/img/ic_back.png"></image>
      </view>
      <view class="jtop_c">个人头像</view>
      <view class="jtop_r" @click="jshow_pop">
        <image src="../../static/img/doc/ic_ellipsis.png"></image>
      </view>
    </view>
    <view class="tophr"></view>

    <image
      :src="
        user.userInfoResponse.avatar
          ? user.userInfoResponse.avatar
          : '../../static/img/Default_avatar.png'
      "
      class="jhead_portrait"
      @click="jshow_pop"
    ></image>

    <!-- 弹出层 -->
    <view :class="[ashowpop ? 'tc' : 'tc hide']">
      <view class="jtop_bj" @click="jshow_pop"></view>

      <!-- 分享 -->
      <view class="tc_fbox">
        <view class="umt" @click="changeAvatar">
          拍照
        </view>
        <view class="umt noborder" @click="changeAvatar2">
          从相册中选取
        </view>
        <view class="tc_fbox_bt" @click="jshow_pop">
          取消
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState, mapMutations } from 'vuex';

import {
  pathToBase64,
  base64ToPath,
} from '../../components/image-tools/index.js';

export default {
  data() {
    return {
      repeat_click: true,
      ashowpop: false,
      user: {},
    };
  },

  onShow() {
    this.user = uni.getStorageSync('user');
    if (!this.user.avatar) {
      this.user.avatar = '../../static/img/Default_avatar.png';
    }
    console.log(this.avatar);
  },

  computed: {
    ...mapState(['hasLogin', 'forcedLogin']),
  },
  methods: {
    jshow_pop() {
      this.ashowpop = !this.ashowpop;
    },
    ...mapMutations(['logout']),
    bindLogin() {
      uni.navigateTo({
        url: '../login/login',
      });
    },
    bindLogout() {
      this.logout();
      /**
       * 如果需要强制登录跳转回登录页面
       */
      if (this.forcedLogin) {
        uni.reLaunch({
          url: '../login/login',
        });
      }
    },
    navigateBack() {
      uni.navigateBack();
    },

    changeAvatar() {
      console.log(111111);
      var that = this;
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera'], //从相册选择、摄像头
        success: function(res) {
          console.log(
            'aaaa0:',
            JSON.stringify(res.tempFilePaths, res.tempFiles[0]),
          );
          console.log('aaaa:', res.tempFiles);

          that.sctp(res.tempFilePaths, res.tempFiles[0]);
        },
      });
    },
    changeAvatar2() {
      console.log(222222);
      var that = this;
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择、摄像头
        success: function(res) {
          console.log(JSON.stringify(res.tempFilePaths, res.tempFiles[0]));
          that.sctp(res.tempFilePaths, res.tempFiles[0]);
        },
      });
    },
    sctp(a, b) {
      console.log('cccc:', a, b);

      var als = '';
      var tpbt = b.name;
      if (tpbt == undefined || tpbt == 'undefined') {
        tpbt = b.path;
      }
      
      var pos = tpbt.lastIndexOf('.');
      console.log('laset222:', pos);
      var suffix = tpbt.substring(pos);
      function random_string(len) {
        len = len || 32;
        var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
          pwd += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
      }
      var g_object_name = random_string(10) + suffix;

      // 获取信息
      request({
        url: '/biz/oss/policy',
        method: 'GET',

        success: res => {
          if (res.data.success == true) {
            console.log('333333', res);

            als = res.data.data;
            request({
              filePath: a[0],
              oss: true,
              formData: {
                name: tpbt,
                key: als.dir + g_object_name,
                policy: als.policy,
                OSSAccessKeyId: als.accessid,
                signature: als.signature,
                callback: als.callback,
                success_action_status: '200',
              },

              success: res => {
                var cc = JSON.parse(res.data);
                console.log('上传图片', cc);
                if (cc.code == '00000') {
                  // var lstp = cc.data.filename;
                  // var lstp2 = cc.data.url;
                  var lstp = tpbt;
                  var lstp2 = cc.data.url + '/' + als.dir + g_object_name;

                  request({
                    url: '/ucenter/user/modify',
                    data: {
                      avatar: lstp2,
                    },
                    header: {
                      'Content-Type': 'application/json',
                      token: `${uni.getStorageSync('access_token')}`,
                    },

                    method: 'PUT',
                    success: openIdRes => {
                      console.log('保存头像', openIdRes);

                      // 已经授权了、查询到用户的数据了
                      if (openIdRes.data.success == true) {
                        this.ashowpop = false;

                        this.user.userInfoResponse.avatar = lstp2;

                        var aa = uni.getStorageSync('user');
                        aa.userInfoResponse['avatar'] = lstp2;
                        uni.setStorageSync('user', aa);

                        this.repeat_click = true;
                        uni.showToast({
                          title: '修改成功',
                        });
                      } else {
                        // 错误
                        this.repeat_click = true;
                        uni.showToast({
                          title: openIdRes.data.msg,
                          icon: 'none',
                        });
                        this.isLoading = false;
                      }
                    },
                    fail: () => {
                      this.repeat_click = true;
                      uni.showToast({
                        title: '服务器开小差，请稍后~',
                        icon: 'none',
                      });
                      return false;
                    },
                  });
                } else {
                  this.repeat_click = true;
                  uni.showToast({
                    title: cc.message,
                    icon: 'none',
                  });
                }
              },
            });
          }
        },
      });
    },
  },
};
</script>

<style>
.umt {
  width: 100%;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-bottom: 1px solid #dddddd;
}

.tc {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  display: block;
}

.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
}

.tc_box {
  width: 540rpx;
  height: auto;
  background: #ffffff;
  border-radius: 14rpx;
  margin: 460rpx 105rpx 0;
  position: relative;
  z-index: 10001;
  overflow: hidden;
}

.tc_boxt {
  width: 100%;
  height: 44rpx;
  font-size: 36rpx;
  line-height: 44rpx;
  display: block;
  float: left;
  text-align: center;
  margin: 40rpx 0 20rpx;
  color: #000000;
}

.tc_boxtc {
  width: 100%;
  height: 36rpx;
  font-size: 28rpx;
  line-height: 36rpx;
  display: block;
  float: left;
  text-align: center;
  margin: 0rpx 0 30rpx;
  color: #666666;
}

.tc_boxi {
  width: 460rpx;
  height: 88rpx;
  background: #ffffff;
  border-radius: 8rpx;
  border: 1px solid #dddddd;
  display: block;
  float: left;
  margin: 0rpx 40rpx 22rpx 40rpx;
}

.tc_boxi input {
  width: 90%;
  height: 48rpx;
  margin: 20rpx 5%;
  font-size: 32rpx;
  color: #000000;
}

.tc_f {
  width: 100%;
  height: 100rpx;
  display: block;
  float: left;
  border-top: 1px solid #dcdee3;
  margin: 20rpx 0 0;
}

.tc_fl {
  width: 269rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  font-size: 36rpx;
  color: #256aff;
  display: block;
  float: left;
  border-right: 1px solid #dcdee3;
}

.tc_fr {
  width: 269rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  font-size: 36rpx;
  color: #256aff;
  display: block;
  float: left;
  opacity: 0.5;
}

.tc_fbox {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 10001;
}

.tc_fboxs {
  width: 100%;
  height: auto;
  display: block;
  float: left;
}

.tc_fbox_ic {
  width: 33.3333%;
  display: block;
  float: left;
  text-align: center;
  font-size: 28rpx;
  color: #666666;
}

.tc_fbox_ic image {
  width: 72rpx;
  height: 72rpx;
  display: block;
  float: none;
  margin: 60rpx auto 24rpx;
}

.tc_fbox_ic text {
  width: 100%;
  height: auto;
  display: block;
  float: none;
  margin: 0 auto 64rpx;
}

.tc_fbox_bt {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 20rpx solid #f2f2f2;
}

.tc_list {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
}

.tc_list_t {
  width: 100%;
  height: 80rpx;
  display: block;
  float: left;
  background: #ffffff;
  font-size: 24rpx;
  color: #999999;
  line-height: 80rpx;
  margin: 0rpx 0;
  border-bottom: 1px solid #dddddd;
  text-align: center;
}

.tc_list {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  border-bottom: 1px solid #dddddd;
}

.tc_list1 {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 36rpx;
  color: #000000;
  line-height: 44rpx;
  margin: 34rpx 28rpx 38rpx 30rpx;
}

.tc_listtwo {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 28rpx;
  color: #999999;
  line-height: 36rpx;
  margin: 42rpx 0rpx 0rpx 0rpx;
}

.tc_list image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 36rpx 30rpx 0 0;
}

.jhead_portrait {
  width: 100%;
  height: 750rpx;
  display: block;
  float: left;
  margin: 220rpx 0 0 0;
}

.index {
  background: #1d1e3e;
  width: 100%;
  height: auto;
}

.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}

.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}

.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}

.jtop_lt {
  margin: 0 0 0 10rpx;
}

.jtop_r {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 20rpx 20rpx 0 0;
}

.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}

.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}

.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}

.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}

.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}

.tophr {
  width: 100%;
  height: 88rpx;
}
</style>
